﻿using System;
using SolidDemo.Business.Interfaces;
using SolidDemo.Data.Contexts;
using SolidDemo.Data.Enums;
using SolidDemo.Data.Shared;

namespace SolidDemo.Business.Repositories
{
    public class LogRepository : GomtuuRepository, ILogRepository
    {
        public LogRepository(MainContext db, IDataHelper dataHelper, IMessageHelper messageHelper): base(db, dataHelper, messageHelper){}

        public void AddLogEntry(string message, string priority, bool isSendEmail = false)
        {
            if (priority != ELogPriority.Info) message = string.Format("{0}\r\n\r\n{1}", message, Environment.StackTrace);

            var log = new Log { Message = message, Priority = priority };

            _db.Logs.Add(log);
            _db.SaveChanges();

            if (isSendEmail) _messageHelper.SendErrorEmail(message, priority);
        }
    }
}
